import { ClientUtil } from "./ClientUtil";
import { Entry } from "./Codes/Model/Share/Entry";
import { CoroutineLockComponent } from "./Core/Module/CoroutineLock/CoroutineLockComponent";
import { EventSystem } from "./Core/Module/EventSystem/EventSystem";
import { IdGenerater } from "./Core/Module/IdGenerater/IdGenerater";
import { Log } from "./Core/Module/Log/Log";
import { Logger } from "./Core/Module/Log/Logger";
import { ObjectPool } from "./Core/Module/ObjectPool/ObjectPool";
import { TimeInfo } from "./Core/Module/Time/TimeInfo";
import { TimerComponent } from "./Core/Module/Timer/TimerComponent";
import { Game } from "./Core/Singleton/Game";
import { GetAssemblyTypes } from "./Decorator/Decorator";
import { FairyGUIExtentions } from "./Support/Client/Extentions/FairyGUIExtentions";
import { GlobalExtentions } from "./Support/Share/Extentions/GlobalExtentions";
import { ETTask } from "./Support/Share/Lib/Async/ETTask";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameEntry extends cc.Component {

    protected start(): void {
        ClientUtil.Init();

        Game.AddSingleton<TimeInfo>(TimeInfo);
        Game.AddSingleton<Logger>(Logger);
        Game.AddSingleton<ObjectPool>(ObjectPool);
        Game.AddSingleton<IdGenerater>(IdGenerater);
        Game.AddSingleton<EventSystem>(EventSystem);
        Game.AddSingleton<TimerComponent>(TimerComponent);
        Game.AddSingleton<CoroutineLockComponent>(CoroutineLockComponent);

        ETTask.ExceptionHandler = Log.Error;

        let types = GetAssemblyTypes();
        EventSystem.Instance.Add(types);

        this.startGame();
    }

    protected update(dt: number): void {
        Game.Update();
    }

    protected lateUpdate(dt: number): void {
        Game.LateUpdate();
        Game.FrameFinishUpdate();
    }

    protected onDestroy(): void {
        Game.Close();
    }

    async startGame() {
        GlobalExtentions.Create();
        FairyGUIExtentions.Create();

        Entry.Init();
        Entry.Start();
    }

}
